package com.qiwenshare.fund.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.qiwenshare.fund.model.FundInfo;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName DataUtil
 * @Description TODO
 * @Author peng.zhang
 * @Date 16:36 2023/6/12
 * @Version 1.0
 **/
@Slf4j
public class FundDataUtil {
    public static List<FundInfo> getFoundList() {
        String url = "https://stock.xueqiu.com/v5/stock/screener/fund/list.json?";
        List<FundInfo> list = new ArrayList<>();
        HashMap param = new HashMap();
        param.put("type", 18);
        param.put("parent_type", 1);
        param.put("order", "desc");
        param.put("order_by", "percent");
        param.put("page", 1);
        param.put("size", 1000);
        HttpRequest request = HttpUtil.createGet(url);
        request.cookie("xqat=1ce5d8004d990273892c085f2b8dc832edd23fde;");
        String result = request.form(param).execute().body();
        log.info(result);
        JSONObject jsonObject = JSONObject.parseObject(result).getJSONObject("data");
        JSONArray jsonArray = jsonObject.getJSONArray("list");
        for (Object object:jsonArray) {
            FundInfo fundInfo = new FundInfo();
            JSONObject item = (JSONObject) object;
            fundInfo.setSymbol(item.getString("symbol"));
            fundInfo.setCode(fundInfo.getSymbol().substring(2));
            fundInfo.setName(item.getString("name"));
            fundInfo.setMarketCapital(item.getDouble("market_capital"));
            fundInfo.setTotalShares(item.getDouble("total_shares"));
            list.add(fundInfo);
        }
        return list;
    }

    public static void main(String[] args) {
        getFoundList();
    }
}
